<?php

namespace App\Http\Controllers\Admin;

use App\Logic\Admin\UsermenberLogic;
use App\Models\Admin;
use App\Repository\AdminRepostiry;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public $admin;

    /**
     * AdminController constructor.
     * @param AdminRepostiry $admin
     */
    public function __construct(AdminRepostiry $admin)
    {
        $this->admin = $admin;
    }

    /**
     * 显示后台首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        //$a = auth()->guard('admin')->user()->id;
        //dd($a);
        $collects = $this->admin->rdashboard();
        return view('admin.dashboard.index', compact('collects'));
    }

    /**
     * 显示后台管理员个人信息
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     * @throws
     */
    public function admininfo(Request $request)
    {
        // 外部参数
        $status = [];
        $Admin = [];
        $id = $request->route('id');
        $act = $request->route('act',"add");
        $Usermenber = new UsermenberLogic($request);

        // 查询数据
        if($id>0){
            $Admin = $Usermenber->one($id);
            $Admin->toArray();
        }

        //提交数据
        if ($request->isMethod('post')) {

            // 操作提交
            $status = $act=="edit" ? $Usermenber->save($Admin) : $Usermenber->insert();

            // 跳转
            if ($status===true) {
                return redirect("/admin/usermember/index");
            }
        }

        return view('admin.admin.index')->with("admininfo", $Admin)->withErrors($status);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deladmin(Request $request){
        $Usermenber = new UsermenberLogic($request);
        $data = $Usermenber->del($request->id);
        return response()->json(['data' => $data]);
    }

    /**
     * 显示用户信息页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function adminlist()
    {
        $Admin = Admin::orderBy('id','desc')->paginate(10);
        return view('admin.usermember.index', ['admins' => $Admin]);
    }

    /**
     * 显示文章列表页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function articleshow()
    {
        return view('admin.article.index');
    }

    /**
     * 创建文章页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function uploadarticle()
    {
        return view('admin.article.create');
    }
}

